﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;

namespace blackhouse.BlackApart.Business.Prices {
    public class DaPriceRabat : IPriceRabat {

        #region Fields

        private ImpPriceRabat instance;
        private IPricesDataAccess dataAccess;
        private int rabatsForApartments;

        #endregion

        #region Properties

        public string RabatKey { get { return this.instance.RabatKey; } }
        public string RabatName { get { return this.instance.RabatName; } }
        public float RabatValue { get { return this.instance.RabatValue; } }
        public bool RabatHasBeenOverridden { get { return this.instance.RabatHasBeenOverridden; } }

        #endregion

        #region Constructors

        internal DaPriceRabat(IPricesDataAccess dataAccess, PriceRabatData data) {
            this.dataAccess = dataAccess;
            this.instance = this.Convert(data);
        }

        public DaPriceRabat(IPricesDataAccess dataAccess, int apartmentTypeId) {
            this.rabatsForApartments = apartmentTypeId;
            this.dataAccess = dataAccess;
            this.instance = new ImpPriceRabat();
        }

        #endregion

        #region Methods

        private ImpPriceRabat Convert(PriceRabatData data) {
            ImpPriceRabat imp = new ImpPriceRabat();
            imp.RabatKey = data.RabatKey;
            imp.RabatName = data.RabatName;
            imp.RabatValue = data.RabatValue;
            return imp;
        }

        public IPriceRabat GetRabat(string rabatKey) {
            if (!this.instance.HaveRabats)
                this.instance.SetRabats(GetDaRabats());
            return this.instance.GetRabat(rabatKey);

        }

        private IPriceRabat[] GetDaRabats() {
            PriceRabatData[] data = this.dataAccess.GetPriceRabats(CultureInfo.CurrentCulture.Name, this.rabatsForApartments);
            DaPriceRabat[] rabats = new DaPriceRabat[data.Length];
            for (int xd = 0; xd < data.Length; xd++) {
                rabats[xd] = new DaPriceRabat(this.dataAccess, data[xd]);
                if (this.instance.HaveRabats)
                    rabats[xd].instance.rabats = this.instance.rabats;
            }
            return rabats;
        }

        public void OverrideRabatValue(float rabatValue) {
            this.instance.OverrideRabatValue(rabatValue);
        }

        #endregion

    }
}
